﻿using System;
using System.Collections.Generic;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class HYPipeConnectFixtures : HYExternalCommand
	{
		public override Result HYExecute(ref string TranName)
		{
			ISelectionFilter pipingAndEquipmentFilter = new PipeAndFixturesSelectionFilter();
			ISelectionFilter pipingFilter = new PipeSelectionFilter();
			ISelectionFilter equipmentFilter = new FixturesSelectionFilter();
			HYPipeConnect hypipeConnect = new HYPipeConnect();
			TranName = "水管连接器具";
			int num = 0;
			Result result;
			for (;;)
			{
				SubTransaction subTransaction = new SubTransaction(base.Doc);
				try
				{
					if (num != 0)
					{
						RevitVersionFuncs.ClearSelection(base.Sel);
					}
					num++;
					IList<Pipe> list = new List<Pipe>();
					FamilyInstance familyInstance = null;
					IList<FamilyInstance> list2 = new List<FamilyInstance>();
					SelectionTypePara selectionTypePara = new SelectionTypePara(BuiltInCategory.OST_PipeCurves, BuiltInCategory.OST_PlumbingFixtures, pipingFilter, equipmentFilter, pipingAndEquipmentFilter, "请框选水管", "请框选卫浴装置", "请框选水管和卫浴装置");
					List<Element> list3 = RevitAPIUIOperate.SelectionCheck(base.Doc, base.Sel, selectionTypePara);
					int num2 = 0;
					foreach (Element element in list3)
					{
						if (element is Pipe)
						{
							list.Add(element as Pipe);
						}
						else if (element is FamilyInstance)
						{
							list2.Add(element as FamilyInstance);
							familyInstance = (element as FamilyInstance);
							num2++;
						}
					}
					subTransaction.Start();
					if (familyInstance != null || list.Count >= 1)
					{
						if (familyInstance == null || list.Count < 1)
						{
							YJKMessageBox.Information("至少选择一个卫浴装置和水管");
						}
						else if (num2 == 1)
						{
							if (!hypipeConnect.NewBeatchPipeEquipment(familyInstance, ref list, 0))
							{
								YJKMessageBox.Information("连接失败");
							}
						}
						else if (num2 > 1 && !hypipeConnect.NewBeatchPipeEquipmentS(familyInstance, null))
						{
							YJKMessageBox.Information("连接失败");
						}
					}
					RevitVersionFuncs.ClearSelection(base.Sel);
					base.Doc.Regenerate();
					subTransaction.Commit();
					continue;
				}
				catch (Autodesk.Revit.Exceptions.OperationCanceledException)
				{
					result = Autodesk.Revit.UI.Result.Succeeded;
				}
				catch (Exception ex)
				{
					YJKMessageBox.Error(ex.Message);
					continue;
				}
				finally
				{
					if (subTransaction != null)
					{
						TransactionStatus status = subTransaction.GetStatus();
						if (status == (TransactionStatus)1)
						{
							subTransaction.Commit();
						}
						else if (status == (TransactionStatus)5)
						{
							subTransaction.RollBack();
						}
					}
				}
				break;
			}
			return result;
		}
	}
}
